package com.guo.springboot.rabbitmq.receiver;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * mq监听
 * @author guo
 * @date 2021/11/30 19:38 星期二
 **/
@Slf4j
@Component
public class RabbitReceiver {


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "queue-springboot-1",durable = "true"),
                    exchange = @Exchange(
                            value = "exchange-springboot-1",
                            type = "topic",
                            durable = "true",
                            ignoreDeclarationExceptions = "true"
                    ),
                    key = "springboot.*"
            )
    )
    @RabbitHandler
    public void onMessage(Message message, Channel channel) throws IOException {

        log.info("----------------------------------------------");
        log.info("消费端Payload：" + message.getPayload());
        Long deliveryTag = (Long) message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        //手工ACK
        channel.basicAck(deliveryTag,false);

    }

}
